'use strict'

class showTecho {

    constructor() {
        this.param = '/'
    }

    async list(c) {
        try {
            var openid = c.box.user.openid;
            let sqltext1 = `select * from techo where openid = '${openid}'`
            let r1 = await c.service.psql.query(sqltext1);
            if(r1.rowCount==0){
                return c.send('null')
            }
            console.log(r1.rows[0].techo_id);
            var userTecho = [];
            for(var i =0;i<r1.rowCount;i++){
                var sticker = await c.service.psql.query(`select * from techo_sticker where techo_id = ${r1.rows[i].techo_id}`);
                var shape = await c.service.psql.query(`select * from techo_shade where techo_id = ${r1.rows[i].techo_id}`);
                var word = await c.service.psql.query(`select * from techo_word where techo_id = ${r1.rows[i].techo_id}`);
                var picture = await c.service.psql.query(`select * from techo_picture where techo_id = ${r1.rows[i].techo_id}`);
                var techoObj = {
                    title: r1.rows[i],
                    sticker:sticker.rows,
                    shape:shape.rows,
                    word:word.rows,
                    picture:picture.rows
                }
                userTecho.push(techoObj);
            }
            return c.send(JSON.stringify(userTecho));
        } catch (err) {
            console.log(err);
            return c.status(500).send('failed')
        }
    }
}

module.exports = showTecho